package com.zhsq.zlc.util;

public class PageUtil {
	
	public static String getPageContent(String url,int pageCurrent,int pageSize,int pageCount){
		if (pageCount == 0) {
			return "";
		}
		String urlNew = url.replace("{pageSize}", pageSize+"").replace("{pageCount}", pageCount+"");
		
		String first = urlNew.replace("{pageCurrent}", 1+"");
		String prev = urlNew.replace("{pageCurrent}", (pageCurrent - 1)+"");
		String next = urlNew.replace("{pageCurrent}", (pageCurrent + 1)+"");
		String last = urlNew.replace("{pageCurrent}", pageCount+"");
		
		StringBuffer html = new StringBuffer();
		html.append("<li class=\"footable-page-arrow"+(pageCurrent<=1?" disabled":"")+"\"><a href=\""+(pageCurrent<=1?"#":first)+"\">«</a></li>");
		html.append("<li class=\"footable-page-arrow"+(pageCurrent<=1?" disabled":"")+"\"><a href=\""+(pageCurrent<=1?"#":prev)+"\">‹</a></li>");

		int pageStartIndex=pageCurrent-1;
		int pageEndIndex=pageCount>(pageCurrent+9)?(pageCurrent+9):pageCount;

		for(int i = pageStartIndex ;i < pageEndIndex; i++){
			String urlItem = urlNew.replace("{pageCurrent}", (i+1)+"");
			html.append("<li class=\"footable-page"+(((i+1) == pageCurrent)?" active":"")+"\"><a href=\""+urlItem+"\">"+(i+1)+"</a></li>");
		}
		html.append("<li class=\"footable-page-arrow"+(pageCurrent==pageCount?" disabled":"")+"\"><a href=\""+(pageCurrent==pageCount?"#":next)+"\">›</a></li>");
		html.append("<li class=\"footable-page-arrow"+(pageCurrent==pageCount?" disabled":"")+"\"><a href=\""+(pageCurrent==pageCount?"#":last)+"\">»</a></li>");
		
		return html.toString().replaceAll("null", "");
	}


	public static String getDialogContent(String url,int pageCurrent,int pageSize,int pageCount){

		if (pageCount == 0) {
			return "";
		}
		//dialogShowPersonManage_{pageCurrent}_{pageSize}_{pageCount}
		String urlNewAll = url.replace("{pageSize}", pageSize+"").replace("{pageCount}", pageCount+"");
		String[] urlNews=urlNewAll.split("//?");
		String urlNew =urlNews[0];
		String first = urlNew.replace("{pageCurrent}", 1+"");
		String[] firsts=first.split("_");
		String prev = urlNew.replace("{pageCurrent}", (pageCurrent - 1)+"");
		String[] prevs=prev.split("_");
		String next = urlNew.replace("{pageCurrent}", (pageCurrent + 1)+"");
		String[] nexts=next.split("_");
		String last = urlNew.replace("{pageCurrent}", pageCount+"");
		String[] lasts=last.split("_");
		StringBuffer html = new StringBuffer();
		html.append("<li class=\"footable-page-arrow"+(pageCurrent<=1?" disabled":"")+"\"><a href=\"#\" onclick=\""+(pageCurrent<=1?firsts[0]+"()":firsts[0]+"("+firsts[1]+"_"+firsts[2]+"_"+firsts[3]+")")+"\">«</a></li>");
		//html.append("<li class=\"footable-page-arrow"+(pageCurrent<=1?" disabled":"")+"\"><a href=\""+(pageCurrent<=1?"#":first)+"\">«</a></li>");
		html.append("<li class=\"footable-page-arrow"+(pageCurrent<=1?" disabled":"")+"\"><a href=\"#\" onclick=\""+(pageCurrent<=1?prevs[0]+"()":prevs[0]+"("+prevs[1]+"_"+prevs[2]+"_"+prevs[3]+")")+"\">«</a></li>");
		//html.append("<li class=\"footable-page-arrow"+(pageCurrent<=1?" disabled":"")+"\"><a href=\""+(pageCurrent<=1?"#":prev)+"\">‹</a></li>");

		int pageStartIndex=pageCurrent-1;
		int pageEndIndex=pageCount>(pageCurrent+9)?(pageCurrent+9):pageCount;

		for(int i = pageStartIndex ;i < pageEndIndex; i++){
			String urlItem = urlNew.replace("{pageCurrent}", (i+1)+"");
			String[] urlItems=urlItem.split("_");
			html.append("<li class=\"footable-page"+(((i+1) == pageCurrent)?" active":"")+"\"><a href=\"#\" onclick=\""+(pageCurrent<=1?urlItems[0]+"()":urlItems[0]+"("+urlItems[1]+"_"+urlItems[2]+"_"+urlItems[3]+")")+"\">\"+(i+1)+\"</a></li>");
			//html.append("<li class=\"footable-page"+(((i+1) == pageCurrent)?" active":"")+"\"><a href=\""+urlItem+"\">"+(i+1)+"</a></li>");
		}
		html.append("<li class=\"footable-page-arrow"+(pageCurrent==pageCount?" disabled":"")+"\"><a href=\"#\" onclick=\""+(pageCurrent==pageCount?nexts[0]+"()":nexts[0]+"("+nexts[1]+"_"+nexts[2]+"_"+nexts[3]+")")+"\">›</a></li>");
		//html.append("<li class=\"footable-page-arrow"+(pageCurrent==pageCount?" disabled":"")+"\"><a href=\""+(pageCurrent==pageCount?"#":next)+"\">›</a></li>");

		html.append("<li class=\"footable-page-arrow"+(pageCurrent==pageCount?" disabled":"")+"\"><a href=\"#\" onclick=\""+(pageCurrent==pageCount?lasts[0]+"()":lasts[0]+"("+lasts[1]+"_"+lasts[2]+"_"+lasts[3]+")")+"\">»</a></li>");
		//html.append("<li class=\"footable-page-arrow"+(pageCurrent==pageCount?" disabled":"")+"\"><a href=\""+(pageCurrent==pageCount?"#":last)+"\">»</a></li>");

		return html.toString().replaceAll("null", "");
	}
}